//
//  CYTrendViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYOrderViewController.h"
#import "CYOrderDetailViewController.h"
#import "CYBettingViewController.h"
#import "CYSegmentView.h"
#import <AudioToolbox/AudioToolbox.h>
#import "CYAudio.h"
#import "CYOrderCollectionCell.h"
@interface CYOrderViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,CYOrderCollectionCellDelegate>{
    BOOL refresh;
}
@property (nonatomic, strong) CYSegmentView *seg; // 选择
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) int currentIndex;
@end

@implementation CYOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _currentIndex = 100;
    self.seg.hidden = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
}

/*懒加载*/
- (CYSegmentView *)seg{
    if (!_seg) {
        
        WS(self)
        _seg = [[CYSegmentView alloc]initWithtitles:@[@"全部订单", @"已中奖", @"待开奖", @"已撤单"] size:15 selectColor:THEME_COLOR defaultSelectedIndex:0 block:^(NSUInteger index) {
            [weakSelf changeTrendStyle:index];
        }];
        [self.view addSubview:_seg];
        
        [_seg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(NAVIBAR_HEIGHT + STATUS_HEIGHT);
            make.centerX.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, _seg.bounds.size.height));
        }];
    }
    return _seg;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        layout.minimumInteritemSpacing = 0;// 垂直方向的间距
        layout.minimumLineSpacing = 0; // 水平方向的间距
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[CYOrderCollectionCell class] forCellWithReuseIdentifier:@"CYOrderCollectionCell1"];
        [_collectionView registerClass:[CYOrderCollectionCell class] forCellWithReuseIdentifier:@"CYOrderCollectionCell2"];
        [_collectionView registerClass:[CYOrderCollectionCell class] forCellWithReuseIdentifier:@"CYOrderCollectionCell3"];
        [_collectionView registerClass:[CYOrderCollectionCell class] forCellWithReuseIdentifier:@"CYOrderCollectionCell4"];
        
        [self.view addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(_seg.bounds.size.height + NAVIBAR_HEIGHT + STATUS_HEIGHT, 0, TABBAR_HEIGHT, 0));
        }];
        
    }
    return _collectionView;
}

/*设置摇一摇功能*/
- (void)setupShakeFunc{
    //允许摇一摇功能
    [UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
    //让自己成为第一响应者
    [self becomeFirstResponder];
}

/*切换注单的类型*/
- (void)changeTrendStyle:(NSUInteger)index{
    [_collectionView setContentOffset:CGPointMake(SCREEN_WIDTH * index, _collectionView.contentOffset.y) animated:YES];
    [self setupCellShake];
}

//摇一摇
- (void)setupCellShake{
    if ([CYAudioManager shareCYAudioManager].openAlert){
        for (CYOrderCollectionCell *cell in [_collectionView subviews]) {
            if (cell.tag == 200 + _seg.currentIndex) {
                if ([cell respondsToSelector:@selector(shake)])
                    [cell shake];
            }
        }
    }
}

#pragma - mark - 摇一摇相关方法
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if ([CYAudioManager shareCYAudioManager].openAlert){
        [CYAudio playAlertWithResourceName:@"shake" ofType:@"wav"];
        [self setupCellShake];
    }
    //设置播放音效
    DeLog(@"开始摇动");
    return;
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    DeLog(@"取消摇动");
    return;
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.subtype == UIEventSubtypeMotionShake) { // 判断是否是摇动结束
        DeLog(@"摇动结束");
    }
    return;
}

#pragma - mark - UICollectionViewDataSource
/** 每组cell的个数*/
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

/** cell的内容*/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier = [NSString stringWithFormat:@"CYOrderCollectionCell%lu",indexPath.row+1];
    CYOrderCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    if (!cell)
        cell = [[CYOrderCollectionCell alloc] init];
    cell.status = (int)indexPath.row;
    cell.tag = 200 + indexPath.row;
    cell.delegate = self;
    return cell;
}

#pragma - mark - UICollectionViewDelegateFlowLayout
/** 每个cell的尺寸*/
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - TABBAR_HEIGHT - _seg.bounds.size.height);
}
#pragma - mark - CYOrderCollectionCellDelegat
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _seg.currentIndex = scrollView.contentOffset.x / SCREEN_WIDTH;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self setupCellShake];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (void)selectIndex:(NSIndexPath *)indexPath ID:(int)ID{
    CYOrderDetailViewController *detailVC = [[CYOrderDetailViewController alloc]init];
    detailVC.bet_id = ID;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void )selectOrderBtnWithArray:(NSMutableArray *)array{
    CYBettingViewController *betVC = [[CYBettingViewController alloc]init];
    betVC.lottery_id = @"1";
    betVC.lottery_name = @"重庆时时彩";
    betVC.selectArr = array;
    [self.navigationController pushViewController:betVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (_collectionView)
        [self.collectionView reloadData];
    else
        [self.collectionView reloadData];
}

- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationItem.title = @"投注记录";
    self.navigationItem.leftBarButtonItem = nil;
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

